package sg.simple.snmp.provider.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.log4j.Logger;
import org.snmp4j.Snmp;
import org.snmp4j.Target;
import org.snmp4j.smi.OID;
import org.snmp4j.smi.VariableBinding;
import org.snmp4j.util.DefaultPDUFactory;
import org.snmp4j.util.TableEvent;
import org.snmp4j.util.TableUtils;

public class SNMPTableReader {

	private Logger logger = Logger.getLogger(SNMPTableReader.class);

	public List<TableEvent> readTable(Snmp session, Target target, OID[] oids) {
		TableUtils tableUtils = new TableUtils(session, new DefaultPDUFactory());
		List<TableEvent> tableEvents = tableUtils.getTable(target, oids, null,
				null);

		List<TableEvent> ret = new ArrayList<TableEvent>();

		for (TableEvent event : tableEvents) {
			if (event.isError()) {
				throw new RuntimeException(event.getErrorMessage());
			}
			ret.add(event);
		}
		return ret;
	}

	protected boolean matches(OID oid, VariableBinding v) {
		return v != null && v.getOid().toString().startsWith(oid.toString());
	}
}
